// brandManage/pages/brandIndex/brandIndex.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        name: "入驻商管理中心"
    },
    getUserProfile() {
        console.log(222);
        var that = this;
        common.getUserInfoNew({
            success(res) {}
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            name: options.name,
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.hideShareMenu();
        wx.setNavigationBarTitle({
            title: that.data.name
        });
        that.getBase();
        that.getMyInfo();
        that.getMerchantData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.getMyInfo();
        that.getMerchantData();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var componentStyle = that.data.componentStyle;
                that.setData({
                    componentStyle: {
                        ...componentStyle,
                        color_minor: !res.extra.color_main ? "#FE513B" : res.extra.color_main,
                        color_minor: !res.extra.color_minor ? "#FE513B" : res.extra.color_minor
                    },
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    rp_name: res.extra.rp_name,
                    luck_img: res.extra.luck_img,
                    little_title: res.extra.title,
                    header_bg: res.extra.header_bg ? res.extra.header_bg : that.data.domain + "img/top.png"
                });
            }
        });
    },
    getMyInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                if (res.extra.merchant_status != 2 && res.extra.merchant_status != 3) {
                    wx.showModal({
                        title: "提示",
                        content: "请先申请成为品牌商再使用该功能",
                        complete: function(res) {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }
                    });
                    return;
                }
                that.setData({
                    info: res.extra,
                    esb_a: res.extra.esb_a
                });
            },
            fail: function(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    complete: function(resc) {
                        wx.switchTab({
                            url: "/pages/personal/personal"
                        });
                    }
                });
            }
        });
    },
    toWriteOff: function() {
        wx.navigateTo({
            url: "/brandManage/pages/applyCode/applyCode"
        });
    },
    scancode: function() {
        var that = this;
        wx.scanCode({
            success: res => {
                console.log(res);
                wx.navigateTo({
                    url: "/" + res.path,
                    success() {},
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: "请扫描正确的提货二维码"
                        });
                    }
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: "请扫描正确的提货二维码",
                    showCancel: false
                });
            }
        });
    },
    getMerchantData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Merchant/merchant/merchant-data",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    details: res.extra
                });
            }
        });
    },
    goToLeaderFunction: function(e) {
        var that = this;
        wx.navigateTo({
            url: e.currentTarget.dataset.url + encodeURIComponent(JSON.stringify(that.data.details))
        });
    },
    onPageTap: function(e) {
        var that = this;
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});